package org.jeecg.modules.sensor.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jeecg.common.util.DeviceParam;
import org.jeecg.common.util.Week;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.iot.model.v20180120.InvokeDataAPIServiceRequest;
import com.aliyuncs.iot.model.v20180120.InvokeDataAPIServiceResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

public class SensorUtils {
	public static final String ACCESS_KEY = "LTAI4FkKNcc45DiqT77qDfB4";
	public static final String ACCESS_SECRET = "U0oCy3kibKqiI0hHKEuUzBfYeDOx5w";
	
	public static final String HEARD_TEXT = "acs:iot:*:1208511665462664:serveapi/";
	/**
	 * 分页查询设备数据
	 * 参数
	 * device_name ： 设备名称
	 * startTime ： 开始时间
	 * endTime ： 结束时间
	 * pageNo : 页数
	 * pageSize ： 条数
	 */
	public static final String API_GET_DEVICEDATA = HEARD_TEXT + "getGDataByParam";
	
	public static final String API_GET_W_DEVICEDATA = HEARD_TEXT + "getWDataByParam";
	/**
	 * 通过条件查询设备数据总数
	 * 参数
	 * device_name ： 设备名称
	 * startTime ： 开始时间
	 * endTime ： 结束时间
	 */
	public static final String API_GET_DEVICEDATA_TOTAL = HEARD_TEXT + "getGDataTotalByParam";
	
	public static final String API_GET_W_DEVICEDATA_TOTAL = HEARD_TEXT + "getWDataTotalByParam";
	/**
	 * 获取设备最新数据
	 */
	public static final String API_GET_DEVICEDATA_NEW_TIME = HEARD_TEXT + "getGNewTime";
	
	public static final String API_GET_W_DEVICEDATA_NEW_TIME = HEARD_TEXT + "getWNewTime";
	
	public static final String ALL_SENSOR_ATTRIBUTE = "All_Sensor_Attribute";
	public static final String GROUND_SENSOR_ATTRIBUTE = "Ground_Sensor_Attribute";
	public static final String W_SENSOR_ATTRIBUTE = "W_sensor_Attribute";
	public static final String AIR_SENSOR_ATTRIBUTE = "Air_Sensor_Attribute";
	public static final String WATER_SENSOR_ATTRIBUTE = "Water_Sensor_Attribute";
	public static final String SOIL_SENSOR_ATTRIBUTE = "Soil_Sensor_Attribute";
	public static final String GAS_SENSOR_ATTRIBUTE = "Gas_Sensor_Attribute";
	
	@SuppressWarnings("deprecation")
	public static DefaultAcsClient initSdk() throws ClientException {
		DefaultProfile.addEndpoint("cn-shanghai", "cn-shanghai", "Iot");
		IClientProfile profile = DefaultProfile.getProfile("cn-shanghai", ACCESS_KEY, ACCESS_SECRET);
		DefaultAcsClient client = new DefaultAcsClient(profile); //初始化SDK客户端
		return client;
	}
	
	public static List<Map<Object, Object>> callAPI(String apiName, Map<String, Object> paramMap) {
		InvokeDataAPIServiceRequest request = new InvokeDataAPIServiceRequest();
		request.setApiSrn(apiName);
		// 请求方法 POST
		request.setSysMethod(MethodType.POST);
		if(null != paramMap) { 
			List<InvokeDataAPIServiceRequest.Param> paramList = new ArrayList<InvokeDataAPIServiceRequest.Param>();
			for(Map.Entry<String, Object> entry : paramMap.entrySet()) {
				InvokeDataAPIServiceRequest.Param param = new InvokeDataAPIServiceRequest.Param();
				param.setParamName(entry.getKey());
				param.setParamValue(entry.getValue().toString());
				paramList.add(param);
			}
			// 在线状态s
			request.setParams(paramList);
		}
		List<Map<Object, Object>> result = null;
		try {
			InvokeDataAPIServiceResponse response = initSdk().getAcsResponse(request);
		    System.out.println(response.getSuccess());
		    System.out.println(response.getErrorMessage());
		    System.out.println(response.getRequestId());
		    // 服务API指定的SQL查询结果
		    result = response.getData().getResultList();

		} catch (ClientException ce) {
			System.out.println(ce.getMessage());
			//result.add(new HashMap<Object, Object>().put("total", -1));
		    ce.printStackTrace();
		}
		return result;
	}
	
	public static void main(String[] args) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("device_name", "WHU_sensor_G_002");
		paramMap.put("startTime", "2019-10-11 10:32:08");
		paramMap.put("endTime", "2019-10-12 10:32:08");
		paramMap.put("pageNo", "0");
		paramMap.put("pageSize", "1000");
		
		List<Map<Object, Object>> dataList = SensorUtils.callAPI(SensorUtils.API_GET_DEVICEDATA, paramMap);
		System.out.println(JSON.toJSONString(dataList));
		// 返回的格式,将dataList转为图表所需要的格式
		List<Map<Object, Object>> resultList = new ArrayList<Map<Object,Object>>();
		List<String> chartNameList = new ArrayList<String>();
		Map<String, String[][]> chartDataMap = new HashMap<String, String[][]>();
		int index = 0;
		for(Map<Object, Object> tempDataMap : dataList) {
			String event_time = tempDataMap.get("event_time").toString();
			for(Map.Entry<Object, Object> entry : tempDataMap.entrySet()) {
				if(!entry.getKey().equals("device_name") && !entry.getKey().equals("event_time")) {
					if(index == 0) {
						chartNameList.add(entry.getKey().toString());
					}
					String[][] tempData = chartDataMap.get(entry.getKey().toString());
					if(tempData == null) {
						tempData = new String[dataList.size()][2];
						chartDataMap.put(entry.getKey().toString(), tempData);
					}
					tempData[index][0] = event_time;
					tempData[index][1] = entry.getValue().toString();
				}
			}
			index ++;
		}
		for(String name : chartNameList) {
			Map<Object, Object> tempParamMap = new HashMap<Object, Object>();
			tempParamMap.put("name", name);
			tempParamMap.put("type", "line");
			tempParamMap.put("connectNulls", true);
			tempParamMap.put("data", chartDataMap.get(name));
			resultList.add(tempParamMap);
		}
		System.out.println(JSON.toJSONString(resultList));
		
		//System.out.println(DeviceParam.TEMPERATURE_SENSOR.getParamKey());
	}
}
